package com.qzimp.fujiancuo.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {

    @Bean
    public Docket docket(){
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qzimp.fujiancuo.controller"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());

    }
    private ApiInfo apiInfo() {

        Contact author = new Contact("吴宗波", "地址:http://api.jqrjq.cn", "邮箱：414090297@qq.com");
        return new ApiInfo(
                "闽南厝在线文档",
                "闽南厝在线文档",
                "1.0",
                "",
                author,
                "",
                "",
                new ArrayList()
        );
    }
    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeys= new ArrayList<ApiKey>();
        apiKeys.add(new ApiKey("Authorization", "Authorization", "header"));
        return  apiKeys;
    }
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts=new ArrayList<>();
        securityContexts.add( SecurityContext.builder()
                .securityReferences(defaultAuth())
                // 所有包含"auth"的接口不需要使用securitySchemes
                .forPaths(PathSelectors.regex("^(?!api/userinfo).*$"))
                .build());
        return  securityContexts;
    }
    private List<SecurityReference> defaultAuth() {
        List<SecurityReference> securityReferences=new ArrayList<>();
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;

    }

}
